package com.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import com.example.lizhiinformation.R;
import com.ownload.Httputil;
import com.ownload.Httputil.OnNetWorkBitmpResponse;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;

public class ImageLoader {
	private static HashMap<String, Bitmap> cach = new HashMap<String, Bitmap>();

	public static void cachr(final Context context, final String path,
			final ImageView imageView,final View view) {

		// 从缓存中取图片
		imageView.setTag(path);
		if (cach.get(path) != null) {
			imageView.setImageBitmap(cach.get(path));
			return;
		} else {
			// 从内存中读取
			File imageFile = GetImageFile(context, path);
			if (imageFile.exists()) {
				Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
				imageView.setImageBitmap(bitmap);
				return;
			} else {
				imageView.setImageResource(R.drawable.empty_photo);
				// 下载图片

				
					Httputil.OnworkBitmaDown(path,
							new OnNetWorkBitmpResponse() {

								@Override
								public void OK(Bitmap bitmap) {
									// TODO Auto-generated method stub
									// 放的缓存中
									ImageView imageView = (ImageView) view.findViewWithTag(path);
									if(imageView!=null){
										imageView.setImageBitmap(bitmap);
									}																		
									
									cach.put(path, bitmap);
									deleteCache(context);
									File file = GetImageFile(context, path);
									FileOutputStream out = null;
									try {
										out = new FileOutputStream(file);
										bitmap.compress(
												Bitmap.CompressFormat.PNG, 100,
												out);
									} catch (FileNotFoundException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} finally {
										if (out != null) {
											try {
												out.close();
											} catch (IOException e) {
												// TODO Auto-generated catch
												// block
												e.printStackTrace();
											}
										}
									}
								}

								@Override
								public void ERROR(String error) {
									// TODO Auto-generated method stub
								}
							});
				}
			}
		}

	

	private static File GetImageFile(Context context, String path) {
		File cacheDir = context.getCacheDir();
		String[] split = path.split("/");
		File imagefile = new File(cacheDir, split[split.length - 1]);
		return imagefile;
	}

	private static void deleteCache(Context context) {

		File cacheDir = context.getCacheDir();
		File[] listFiles = cacheDir.listFiles();
		long length = 0;
		for (File file : listFiles) {
			length += file.length();
		}

		if (5 * 1024 * 1024 > length) {
			return;
		}
		Arrays.sort(listFiles, new Comparator<File>() {
			// 1 < 2 负数 1 >2 正数 1=2 0
			@Override
			public int compare(File arg0, File arg1) {
				// TODO Auto-generated method stub

				return (int) (arg0.lastModified() - arg1.lastModified());
			}
		});
		for (int i = 0; i < listFiles.length / 2; i++) {
			listFiles[i].delete();
		}
	}
}
